/**
 * 
 */
package vanhoc.android.medialcool.utils;

import java.io.File;
import java.util.HashMap;

import vanhoc.android.mediakool.R;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.media.RingtoneManager;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.Toast;

/**
 * @author SEVEN
 *
 */
public class RingtoneUtils {

	private Context _context;
	
	
	
	public RingtoneUtils() {
		// TODO Auto-generated constructor stub
	}
	
	
	public RingtoneUtils(Context ct) {
		this._context = ct;
	}


	public void setRingtoneDefault(HashMap<String, String> ringtoneData) {
		ContentValues contenValues = new ContentValues();
		contenValues.put(MediaStore.MediaColumns.DATA, ringtoneData.get("ABSOLUTEPATH"));
		contenValues.put(MediaStore.MediaColumns.TITLE,ringtoneData.get("TITLE"));
		contenValues.put(MediaStore.MediaColumns.SIZE, 215454);
		contenValues.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
		contenValues.put(MediaStore.Audio.Media.ARTIST, ringtoneData.get("ARTIST"));
		contenValues.put(MediaStore.Audio.Media.DURATION, ringtoneData.get("DURATION"));
		contenValues.put(MediaStore.Audio.Media.IS_RINGTONE, true);
		contenValues.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
		contenValues.put(MediaStore.Audio.Media.IS_ALARM, false);
		contenValues.put(MediaStore.Audio.Media.IS_MUSIC, false);
		
		//Insert it into the database
		Uri uri = MediaStore.Audio.Media.getContentUriForPath(ringtoneData.get("ABSOLUTEPATH"));
		
		Log.e("ABSOLUTEPATH", ringtoneData.get("ABSOLUTEPATH"));
		Log.e("uri", uri.toString());
		
		//getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + ringtoneData.get("ABSOLUTEPATH") + "\"", null);
		
		Uri ringtoneUri = this._context.getContentResolver().insert(uri, contenValues);
		
		Log.e("the ringtone uri is", ringtoneUri.toString());
		
		try {
			//I think error at line here
			RingtoneManager.setActualDefaultRingtoneUri(this._context,RingtoneManager.TYPE_RINGTONE, ringtoneUri);
			Toast.makeText(this._context, this._context.getResources().getString(R.string.successfully), Toast.LENGTH_LONG).show();
		} catch (Exception e) {
			Log.d("Loi", "catch exception");
		}	
	}
}
